/**
 * Created by xiongxin on 2017/12/5.
 */


(function () {

    //利用sessionStorage开放一个key来存取json格式的字符串
    var key = '_dbs_ram';

    var Local = {
        set: function (name, value) {
            this.userInfo[name] = value;
            sessionStorage[key] = JSON.stringify(this.userInfo);
        },

        get: function (name) {
            return this.userInfo[name];
        },

        remove: function (name) {
            delete this.userInfo[name];
            sessionStorage[key] = JSON.stringify(this.userInfo);
        },

        clear: function () {
            sessionStorage.removeItem(key);
        },

        userInfo: (function () {
            if (typeof sessionStorage[key] !== 'undefined') {
                return JSON.parse(sessionStorage[key]);
            } else {
                return {};
            }
        })()
    };

    // // 加密
    // function Encrypt(word) {
    //   var key = CryptoJS.enc.Utf8.parse("123654123654");
    //   var iv = CryptoJS.enc.Utf8.parse("123654123654");
    //   var srcs = CryptoJS.enc.Utf8.parse(word);
    //   var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC});
    //   return encrypted.toString();
    // }
    //
    // // 解密
    // function Decrypt(word) {
    //   var key = CryptoJS.enc.Utf8.parse("123654123654");
    //   var iv = CryptoJS.enc.Utf8.parse("123654123654");
    //   var decrypt = CryptoJS.AES.decrypt(word, key, {iv: iv, mode: CryptoJS.mode.CBC});
    //   return CryptoJS.enc.Utf8.stringify(decrypt).toString();
    // }

    window.Local = Local;
})();


function sendService(url, options, fn) {
   
    $.ajax({
        url: url,
        type: 'POST',
        data: options,
        success: function (data) {
            
            if (fn) {
                fn(data);
            }
        },
        error: function (e) {
            //loadPop.hide();
            console.log('显示网络异常！')
        }
    })
}



function pageDataService(vueid,url,options,fn) {

    if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE9.0"){
        // window.location.href="${basePath}test";
    }else{
        var  loading = vueid.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
    }

    
    if (!vueid.parameters){
        vueid.parameters = {};
        vueid.parameters.currentPage= 1;
    }else{
        if (vueid.currentPage){
            vueid.parameters.currentPage=vueid.currentPage
        } else{
            vueid.parameters.currentPage = 1;
        }
    }
    

    $.ajax({
        url: url,
        type: 'POST',
        data: vueid.parameters,
        success: function (data) {
            if (fn) {
                if(loading){
                    loading.close();
                }
                fn(data);
                if(data.status=="error"){
                    return
                }
                vueid.tableData = data.t;
                vueid.totalPage = data.page.totalPage;
                sessionStorage.setItem('totalPage',data.page.totalPage)
                vueid.totalCount = data.page.totalResult;
            }
        },
        error: function (e) {
            //loadPop.hide();
            console.log('显示网络异常！')
        }
    })
}

function pageQuery (vueid,url,options ,fn ) {

    pageDataService(vueid,url,options,fn);

}


/**
 * 分页数据查询
 * @param vueid
 * @param url
 * @param option
 * @param currentPage
 * @param fn
 * @constructor
 */

function Pagesearch (vueid,url, options, currentPage, fn) {
    
    vueid.currentPage = currentPage;
    pageQuery(vueid,url, options, fn);
}


//页面重新加载
function reloadPage(url) {
    $(".main-right").empty();
    $(".main-right").load(url);
}


//格式化时间
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

//处理冒泡存在问题
// function stopMaoPao() {
//     if (event.stopPropagation){
//         // this code is for Mozilla and Opera
//         event.stopPropagation();
//     }else if (window.event) {
//         // this code is for IE
//         window.event.cancelBubble = true;
//     }
// }

function stopMaoPao(event){
    var e=arguments.callee.caller.arguments[0]||event;
    if (e && e.stopPropagation) {
        e.stopPropagation();
    } else if (window.event) {
        window.event.cancelBubble = true;
    }
}

/**
 * 兼容ie9的提示框文字提示
 * @param input  input框#id
 * @param showWords   需要显示的提示名字
 */
function placeHolde(input,showWords) {
    $(input).val(showWords);
    $(input).focus(function () {
        $(input).val("");
    });
    $(input).blur(function () {
        if($(input).val() == ''){
            $(input).val(showWords);
        }
    })
}



